package com.blackink.tool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 简单的分页
 */
public class Paging {

    /**
     * 默认页面数据量
     */
    private  final int pageSize = 10;

    /**
     * 默认显示页面页码
     */
    private  final int pageNumber = 1;

    /**
     * 总记录数
     */
    private final String TOTAL_NUM = "TotalNumber";

    /**
     * 总页数
     */
    private final String PAGE_COUNT = "PageCount";

    public Map paging(List list,Integer pageNumber,Integer pageSize){

        Map map = new HashMap();
        int ls = list.size();
        map.put(TOTAL_NUM,ls);

        if (pageNumber == null || pageNumber <= 0){
            pageNumber = this.pageNumber;
        }
        if (pageSize == null || pageSize <= 0){
            pageSize = this.pageSize;
        }
        map.put("pageSize",pageSize);

        if (ls==0){
            map.put(PAGE_COUNT,1);
            map.put("data",list);
        }else {
            int pc;
            if (ls%pageSize == 0){
                pc = ls/pageSize;
            }else {
                pc = ls/pageSize+1;
            }
            if (pageNumber>pc){
                pageNumber = pc;
            }
            map.put(PAGE_COUNT,pc);

            int firstIndex = (pageNumber - 1) * pageSize;
            int lastIndex = pageNumber * pageSize;
            if (lastIndex > ls){
                lastIndex = ls;
            }
            map.put("pageNum",pageNumber);
            map.put("data",list.subList(firstIndex,lastIndex));
        }

        return map;
    }


}
